package com.hutao.chargingpile.pitfall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hutao.chargingpile.pitfall.pojo.dto.CpHiddenPageDto;
import com.hutao.chargingpile.pitfall.pojo.entity.CpHidden;
import com.hutao.chargingpile.pitfall.result.R;
import com.hutao.chargingpile.pitfall.service.CpHiddenManageService;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 隐患处理
 * @return
 */
/**
 * @作者:卢光云
 * @时间:2023/3/29 11:19
 */
@RestController
@RequestMapping("/cpHidden")
public class CpHiddenManageController {

    @Resource
    private CpHiddenManageService cpHiddenManageService;

    /**
     * 分页、模糊、区间查询
     * @param cpHiddenPageDto
     * @return
     */
    @PostMapping("/pageList")
    public R pageList(@RequestBody CpHiddenPageDto cpHiddenPageDto){
        Page<CpHidden> page = cpHiddenManageService.pageList(cpHiddenPageDto);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageNum",page.getCurrent());
        map.put("pageSize",page.getSize());
        map.put("pageTotal",page.getTotal());
        map.put("list",page.getRecords());
        return R.ok().data(map);
    }

    /**
     * 修改、添加
     * @param cpHidden
     * @return
     */
    @RequestMapping("/updateHidden")
    public R updateHidden(@RequestBody CpHidden cpHidden){
        try {
            cpHiddenManageService.updateHidden(cpHidden);
            return R.ok().message("编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("编辑失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteHidden/{id}")
    public R deleteHidden(@ApiParam(value = "数据id", required = true, example = "1")
                          @PathVariable Long id){
        boolean result = cpHiddenManageService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

}